package com.petcommunity.servlet;

import com.petcommunity.dao.NoticeDAO;
import com.petcommunity.model.Notice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/notices")
public class NoticeServlet extends HttpServlet {

    private NoticeDAO noticeDAO = new NoticeDAO();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Notice> notices = noticeDAO.getAllNotices();
        request.setAttribute("notices", notices);
        request.getRequestDispatcher("notices.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求的字符编码为 UTF-8
        request.setCharacterEncoding("UTF-8");

        Notice notice = new Notice();
        notice.setTitle(request.getParameter("title"));
        notice.setContent(request.getParameter("content"));

        noticeDAO.addNotice(notice);
        response.sendRedirect("notices");
    }
}

